package com.lucence.d_optimize;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

import com.lucence.a_helloword.Article;
import com.lucence.util.ArticleDocument;
import com.lucence.util.Config;

public class TestRAMDirectory {

	@Test
	public void testRAMDirectory() throws Exception{
		Directory ramDirectory = new RAMDirectory();
		IndexWriterConfig ramindexWriterConfig = new IndexWriterConfig(Version.LUCENE_36, Config.getAnalyzer());
		IndexWriter ramindexWriter = new IndexWriter(ramDirectory, ramindexWriterConfig);
		Article article = new Article();
		article.setId("lucene");
		article.setTitle("Lucene is a sousou");
		article.setContent("Lucene is apache");
		ramindexWriter.addDocument(ArticleDocument.articleToDocument(article));
		ramindexWriter.close();
	}
	private volatile int d;
	@Test
	public void testRamDirectoryAndFsDirectory() throws Exception{
		Directory fsDirectory = FSDirectory.open(new File("./indexDir/"));
		Directory ramDirectory = new RAMDirectory(fsDirectory);
		
		IndexWriterConfig ramindexWriterConfig = new IndexWriterConfig(Version.LUCENE_36, Config.getAnalyzer());
		IndexWriter ramindexWriter = new IndexWriter(ramDirectory, ramindexWriterConfig);
		Article article = new Article();
		article.setId("1");
		article.setTitle("Lucene is a sousou");
		article.setContent("Lucene is apache");
		ramindexWriter.addDocument(ArticleDocument.articleToDocument(article));
		ramindexWriter.close();
		
		IndexWriterConfig fsindexWriterConfig = new IndexWriterConfig(Version.LUCENE_36, Config.getAnalyzer());
		fsindexWriterConfig.setOpenMode(OpenMode.CREATE);
		IndexWriter fsindexWriter = new IndexWriter(fsDirectory, fsindexWriterConfig);
		
		fsindexWriter.addIndexes(ramDirectory);
		
		fsindexWriter.close();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
